﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MS.InformationManagement.Core.Support;
using MS.InformationManagement.DAL.DTOs;
using MS.InformationManagement.DAL.DocumentCenter;
using Microsoft.SharePoint;
using MS.InformationManagement.Support.Constants;
using MS.InformationManagement.Core.TransactionsResponse;

namespace MS.InformationManagement.BLL.DocumentPrinter
{
    public class UpdateDeliveryMailCommand : ICommandWithResponse
    {

        #region Fields

        EnvíoDeCorrespondenciaDTO[] _deliveries;

        #endregion

        #region Constructors

        public UpdateDeliveryMailCommand(EnvíoDeCorrespondenciaDTO[] deliveries)
        {
            _deliveries = deliveries;
        }

        #endregion

        #region Command Methods

        public ITransactionResponse Execute()
        {
            UpdateDeliveryCommandResponse response = new UpdateDeliveryCommandResponse();
            response.NewDeliveries = new List<EnvíoDeCorrespondenciaDTO>();

            using (DCDocumentCenterDataContext ctx = new DCDocumentCenterDataContext(SPContext.Current.Web.Url))
            {
                foreach (EnvíoDeCorrespondenciaDTO delivery in _deliveries)
                {
                    MedioDeEnvio deliveryMean = GetDeliveryMean(delivery.DeliveryMeanId, ctx);
                    Zona deliveryZone = GetDeliveryZone(delivery.DeliveryZone, ctx);

                    var deliveryInfo = (from deliveryItem in ctx.EnviosDeCorreo
                                        where deliveryItem.IdDocumento == delivery.DocumentId &&
                                              deliveryItem.BibliotecaDelDocumento == delivery.LibraryName
                                        select deliveryItem).FirstOrDefault();
                    if (deliveryInfo == null)
                        response.NewDeliveries.Add(delivery);
                    else
                    {
                        deliveryInfo.EnvioNacional = delivery.IsNationalDelivery;
                        deliveryInfo.EnvioLocal = delivery.IsLocalDelivery;
                        deliveryInfo.CostoDelEnvío = delivery.DeliveryCost;
                        deliveryInfo.MedioEnvioCorreo = deliveryMean;
                        deliveryInfo.PesoCorrespondencia = delivery.Weight;
                        deliveryInfo.ZonaEnvio = deliveryZone; 
                        ctx.SubmitChanges();
                    }
                }
            }
            return response;
        }

        public void Undo()
        {
        }

        #endregion

        #region Private Methods

        private Zona GetDeliveryZone(int deliveryZone, DCDocumentCenterDataContext context)
        { 
            return context.Zonas.FirstOrDefault(deliveryMean => deliveryMean.Id.Equals(deliveryZone));
        }


        private MedioDeEnvio GetDeliveryMean(int deliveryMeanId, DCDocumentCenterDataContext context)
        {
            MedioDeEnvio entity = null;
            entity = context.MediosDeEnvio.SingleOrDefault(deliveryMean => deliveryMean.Id.Equals(deliveryMeanId));
            if (entity == default(MedioDeEnvio)) { throw new Exception(string.Format("El medio de envio con id {0} no existe", deliveryMeanId)); }
            return entity;
        }


        #endregion
    }
}
